package com.jcf.springboot.controller.business;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.business.FilmInfo;
import com.jcf.springboot.service.FilmInfoService;
import com.jcf.springboot.entity.business.Play;
import com.jcf.springboot.service.PlayService;
import com.jcf.springboot.entity.business.Member;
import com.jcf.springboot.service.MemberService;
import com.jcf.springboot.entity.business.MyOrder;
import com.jcf.springboot.entity.business.vo.PreOrder;
import com.jcf.springboot.service.OrderService;
import com.jcf.springboot.entity.business.Seat;
import com.jcf.springboot.service.SeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjj
 * @since 2021-04-17
 */
@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private SeatService seatService;

    @Autowired
    private PlayService playService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private FilmInfoService filmInfoService;

    @GetMapping("/getPreOrder/{filmId}/{playOpenTime}")
    public R getPreOrder(@PathVariable String filmId, @PathVariable String playOpenTime) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(playOpenTime);
       PreOrder preOrder = orderService.getPreOrder(filmId,date);
       if(preOrder != null){
           return R.ok().data(preOrder).message("获取预定信息成功");
       }
       else {
           return R.error().message("获取预定信息失败");
       }

    }

    @GetMapping("getOrderList")
    public R getOrderList(){
        QueryWrapper<MyOrder> myOrderQueryWrapper = new QueryWrapper<>();
        myOrderQueryWrapper.orderByDesc("order_time");
        List<MyOrder> list = orderService.list(myOrderQueryWrapper);
        if(list != null){
            return R.ok().data(list).message("获取订单列表成功");
        }
        else {
            return R.error().message("获取订单列表失败");
        }

    }

   @GetMapping("getMyOrder")
   public R getMyOrder(String id){
       QueryWrapper<MyOrder> myOrderQueryWrapper = new QueryWrapper<>();
       myOrderQueryWrapper.orderByDesc("order_time");
       myOrderQueryWrapper.eq("member_id", id);
       List<MyOrder> list = orderService.list(myOrderQueryWrapper);
       if(list != null){
           return R.ok().data(list).message("获取我的订单成功");
       }
       else {
           return R.error().message("获取我的订单失败");
       }

   }


    @PostMapping("saveOrder")
    public R saveOrder(@RequestBody MyOrder myOrder){
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("member_phone", myOrder.getPhone());
        Member one = memberService.getOne(memberQueryWrapper);
        if(one==null){
            return R.error().message("手机号输入错误");
        }
        myOrder.setMemberId(one.getMemberId());
        boolean b = cancel(myOrder);
        if(b){

            boolean save = orderService.save(myOrder);
            if(save){
                return R.ok().data(myOrder.getOrderId()).message("添加订单成功");
            }
            else {
                return R.error().message("添加订单失败");
            }
        }
        else {
            return R.error().message("取消订单失败");
        }
    }

    @PostMapping("editOrder")
    public R editOrder(@RequestBody MyOrder myOrder){

            boolean save = orderService.updateById(myOrder);
            if(save){
                return R.ok().data(myOrder.getOrderId()).message("修改订单成功");
            }
            else {
                return R.error().message("修改订单失败");
            }
    }

    @GetMapping("getOrderById")
    public R getOrderById(String orderId){
        QueryWrapper<MyOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        MyOrder one = orderService.getOne(wrapper);
        if(one != null){
            return R.ok().data(one).message("获取订单成功");
        }
        else {
            return R.error().message("获取订单失败");
        }
    }

    @PutMapping("payOrder")
    public R payOrder(@RequestBody MyOrder myOrder){
        UpdateWrapper<MyOrder> wrapper = new UpdateWrapper<>();
        boolean b = orderService.updateById(myOrder);
        if(b){
            return R.ok().message("支付成功");
        }
        else {
            return R.error().message("支付失败");
        }

    }

    @PutMapping("cancelOrder")
    public R cancelOrder(@RequestBody MyOrder myOrder){
        boolean cancel = cancel(myOrder);
        if (cancel){
            return R.ok().message("取消订单成功");
        }
        else {
            return R.error().message("获取订单失败");
        }
    }

    private boolean cancel(MyOrder myOrder){
        QueryWrapper<MyOrder> myOrderQueryWrapper = new QueryWrapper<MyOrder>();
        myOrderQueryWrapper.eq("member_id", myOrder.getMemberId()).eq("order_status", 1);
        MyOrder one = orderService.getOne(myOrderQueryWrapper);
        if(one != null){
            one.setOrderStatus(0);
            return orderService.updateById(one);
        }
       return true;
    }

    @DeleteMapping("removeOrder")
    public R removeOrder(String orderId,String seat,String filmName,String playOpenTime) throws ParseException {

        boolean b = orderService.removeById(orderId);
        QueryWrapper<FilmInfo> filmInfoQueryWrapper = new QueryWrapper<>();
        filmInfoQueryWrapper.eq("film_name", filmName);
        FilmInfo one1 = filmInfoService.getOne(filmInfoQueryWrapper);
        QueryWrapper<Play> playQueryWrapper = new QueryWrapper<>();
        playQueryWrapper.eq("film_id", one1.getFilmId());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = format.parse(playOpenTime);
        playQueryWrapper.eq("play_open_time", date);
        Play one = playService.getOne(playQueryWrapper);
        String playId = one.getPlayId();
        if(b){
            String[] seats = seat.split(",");
            for (String s : seats) {
                int x =  Integer.parseInt(s.substring(0,s.indexOf("排")));
                int y = Integer.parseInt(s.substring(s.indexOf("排")+1,s.indexOf("座")));
                UpdateWrapper<Seat> wrapper = new UpdateWrapper<>();
                wrapper.eq("xaxis", x);
                wrapper.eq("yaxis", y);
                wrapper.eq("play_id", playId);
                Seat seat1 = new Seat();
                seat1.setStatus("0");
                seat1.setXaxis(x);
                seat1.setYaxis(y);
                boolean b1 = seatService.update(seat1, wrapper);
                if(!b1){
                    return R.error().message("座位初始化失败");
                }
            }
            return R.ok().message("删除订单成功");
        }
        else {
            return R.error().message("删除订单失败");
        }
    }
}

